package jsimplesim.view.components;

import jsimplesim.view.*;
import jsimplesim.view.renderers.StackCellRenderer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
import org.apache.log4j.Logger;
import jsimplesim.model.StackModel;

/**
 * @author mb808326
 *
 */
@SuppressWarnings({ "serial", "unused" })
public class StackTable extends JTable implements TableModelListener {

	int lastCol = -1;
    private TableColumn column;
    private JSimpleSimView view;
    static Logger logger = Logger.getLogger(StackTable.class);

    /**
     *
     * @param model
     * @param view1
     */
    public StackTable(StackModel model, JSimpleSimView view1) {
        super(model);
        setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        setShowVerticalLines(false);
        setShowHorizontalLines(false);
        setRequestFocusEnabled(false);
        setAutoCreateRowSorter(true);
        setBackground(Color.WHITE);
        setForeground(Color.BLACK);
        this.view = view1;

        setFont(new Font("monospaced", Font.PLAIN, 12));
        setShowGrid(false);
        this.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        setRowSelectionAllowed(false);
        setIntercellSpacing(new Dimension(0, 0));
        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        setAutoscrolls(true);

        setColumnSelectionAllowed(false);

        setName("stackTable"); 
        
        setRowHeight(19);

        getTableHeader().setReorderingAllowed(false);
      

        // Set the size of each column

        int characterWidth = getFontMetrics(getFont()).charWidth('w');
        int columns = getModel().getColumnCount();

        this.getModel().addTableModelListener(this);

        
            column = getColumnModel().getColumn(0);

         
                column.setPreferredWidth(9 * characterWidth);
           
    

        // Set default cell renderer

        TableCellRenderer stkr = new StackCellRenderer();
        setDefaultRenderer(String.class, stkr);

        // disallow column resizing
        
            column = getColumnModel().getColumn(0);
            column.setResizable(false);

        // Create column specific cell renderer

        TableCellRenderer StackCellRenderer = new StackCellRenderer();

        column = getColumnModel().getColumn(0);
        column.setCellRenderer(StackCellRenderer);
      
        
        HexCellEditor hce = new HexCellEditor();
        hce.setFont(getFont());
        DefaultCellEditor dce = new DefaultCellEditor(hce);
        dce.setClickCountToStart(1);
        setDefaultEditor(String.class, dce);

    }

   
    

    @Override
    public void updateUI() {
        super.updateUI();
        int columnCount = getColumnCount();
        TableColumnModel columnModel = getColumnModel();
        columnModel.getColumn(0).setCellRenderer(new StackCellRenderer());
    }
}


